﻿using System;
using System.Collections.Generic;
using UnityEngine;
using static Enums.Timer;
/// <summary>
/// 值类型扩展
/// </summary>
public static class ValueExtension
{

    /// <summary>
    /// 将数字转为 Boolean，非0为 true
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool ToBool(this int value)
    {
        return value != 0;
    }

    /// <summary>
    /// 根据数值转换成对应长度的自增数值数组
    /// </summary>
    /// <param name="value"></param>
    /// <param name="startValue">起始值</param>
    /// <returns></returns>
    public static int[] ToSelfAddArray(this int value, int startValue = 0)
    {
        var values = new int[value];
        for (int i = 0; i < values.Length(); i++)
        {
            values[i] = startValue + i;
        }
        return values;
    }

    public static List<int> ToSelfAddAList(this int value, int startValue = 0)
    {
        var values = new List<int>(value);
        for (int i = 0; i < value; i++)
        {
            values.Add(startValue + i);
        }
        return values;
    }

    public static string RichTextRedColor<T>(this T value) where T : struct
        => value.RichTextColor(Constant.Color.RED);

    public static string RichTextGreenColor<T>(this T value) where T : struct
        => value.RichTextColor(Constant.Color.GREEN);

    public static string RichTextYellowColor<T>(this T value) where T : struct
        => value.RichTextColor(Constant.Color.TEXT_YELLOW);

    /// <summary>
    /// 格式化数字文本颜色
    /// </summary>
    /// <param name="value">数字文本内容</param>
    /// <param name="size">数字文本颜色</param>
    /// <returns></returns>
    public static string RichTextColor<T>(this T value, string color) where T : struct
    {
        return Utility.RichText.FormatColor(value.ToString(), color);
    }

    /// <summary>
    /// 转换为数字单位文本
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string FormatUnitString(this long value, bool isFightFont = false)
    {
        // 微信小程序读取 asset 值有问题使用 config
        //var gameConfig = ControllerManager.Instance.GameConfigObject;
        //return Utility.Value.FormatUnitString(value,
        //    gameConfig.FormatUnitFormat,
        //    gameConfig.FormatUnitIgnoreValue,
        //    gameConfig.FormatUnitUnitScale,
        //    gameConfig.FormatUnitValueUnitKeys);

        if (value==0)
        {
            return "0";
        }

        var gameConfig = GameApp.Instance;
        return Utility.Value.FormatUnitString(value,
            gameConfig.FormatUnitFormat,
            gameConfig.FormatUnitIgnoreValue,
            gameConfig.FormatUnitUnitScale,
            isFightFont ? gameConfig.FormatUnitLetterUnitKeys : gameConfig.FormatUnitValueUnitKeys);
    }

    /// <summary>
    /// 转换为数字单位文本
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string FormatUnitString(this int value)
    {
        return FormatUnitString((long)value);
    }

    /// <summary>
    /// 时间转文本 单位：秒
    /// </summary>
    /// <param name="second"></param>
    /// <param name="type"></param>
    /// <param name="count"></param>
    /// <param name="languageType"></param>
    /// <param name=""></param>
    /// <returns></returns>
    public static string TimeToString(this int second, TimeFormatType type = TimeFormatType.HH_MM_SS, TimeShowCount count = TimeShowCount.All, TimeFormatLanguageType languageType = TimeFormatLanguageType.Default)
    {
        return string.Empty;
    }

    /// <summary>
    /// 时间转文本 单位：秒
    /// </summary>
    /// <param name="second"></param>
    /// <param name="type"></param>
    /// <param name="count"></param>
    /// <param name="languageType"></param>
    /// <returns></returns>
    public static string TimeToString(this long second, TimeFormatType type = TimeFormatType.HH_MM_SS, TimeShowCount count = TimeShowCount.All, TimeFormatLanguageType languageType = TimeFormatLanguageType.Default)
    {
        return string.Empty;
    }

    /// <summary>
    /// 时间转文本 单位：秒
    /// </summary>
    /// <param name="second"></param>
    /// <param name="type"></param>
    /// <param name="count"></param>
    /// <param name="languageType"></param>
    /// <returns></returns>
    public static string TimeToString(this float second, TimeFormatType type = TimeFormatType.HH_MM_SS, TimeShowCount count = TimeShowCount.All, TimeFormatLanguageType languageType = TimeFormatLanguageType.Default)
    {
        return string.Empty;
    }

    /// <summary>
    /// 毫秒转秒
    /// </summary>
    /// <param name="second"></param>
    /// <returns></returns>
    public static long MsToSecond(this long second)
    {
        if (second == 0)
            return 0;
        return second / Utility.Timer.MsPerSecond;
    }

    /// <summary>
    /// 毫秒转秒
    /// </summary>
    /// <param name="second"></param>
    /// <returns></returns>
    public static long MsToMinute(this long second)
    {
        if (second == 0)
            return 0;
        return second / Utility.Timer.MsPerMinute;
    }

    /// <summary>
    /// 毫秒转小时
    /// </summary>
    /// <param name="second"></param>
    /// <returns></returns>
    public static long MsToHour(this long second)
    {
        if (second == 0)
            return 0;
        return second / Utility.Timer.MsPerHour;
    }

    /// <summary>
    /// 毫秒转天
    /// </summary>
    /// <param name="second"></param>
    /// <returns></returns>
    public static long MsToDay(this long second)
    {
        if (second == 0)
            return 0;
        return second / Utility.Timer.MsPerDay;
    }

    /// <summary>
    /// 秒转毫秒
    /// </summary>
    /// <param name="second"></param>
    /// <returns></returns>
    public static long SecondToMs(this long second)
    {
        if (second == 0)
            return 0;
        return second * Utility.Timer.MsPerSecond;
    }

    /// <summary>
    /// 分转毫秒
    /// </summary>
    /// <param name="second"></param>
    /// <returns></returns>
    public static long MinuteToMs(this long second)
    {
        if (second == 0)
            return 0;
        return second * Utility.Timer.MsPerMinute;
    }

    /// <summary>
    /// 数字转大写数字  如18 = 一十八
    /// </summary>
    /// <param name="value">需要转化的整数</param>
    /// <returns>返回大写数字</returns>
    public static string NumToChi(this int value)
    {
        string res = string.Empty;
        string str = value.ToString();
        string schar = str.Substring(0, 1);
        if (schar != "0")
        {
            res = (Constant.Localization.Digit + schar).FormatLocalize();
        }
        if (str.Length > 1)
        {
            switch (str.Length)
            {
                // 暂时只管两位的了，剩下的有需要再加就是
                case 2:
                case 6:
                    res += (Constant.Localization.Digit + "10").FormatLocalize();
                    break;
                default:
                    break;
            }
            res += NumToChi(int.Parse(str.Substring(1, str.Length - 1)));
        }
        return res;
    }

    /// <summary>
    /// 小数转百分比
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string ToPercent(this float value,string e_params = "F2")
    {
        return Utility.Text.Format("{0}%", value.ToString(e_params));
    }

    /// <summary>
    /// long 绝对值
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static long Abs(this long num)
    {
        if (num < 0)
            num = -num;
        return num;
    }

}
